require "Dialogs"

Arena.Resize(565, 130)
Arena.MoveTo(320, 90, true, true)
Player.sprite.alpha = 0

Black1 = CreateSprite("Bullets/Bones/Body","Top")
Black2 = CreateSprite("Bullets/Bones/Body","Top")
Black1.Scale(640/5, 250)
Black2.Scale(640/5, 230)
Black1.color = {0, 0, 0}
Black2.color = {0, 0, 0}
Black1.MoveTo(320, 355)
Black2.MoveTo(320, 115)

if #Encounter["Items"] <= 0 then
	if GetAlMightyGlobal("Idioma") == "Spanish" then
		Encounter["Items"] = {"[starcolor:7d7d7d][color:7d7d7d]Nada[color:ffffff]"}
	else
		Encounter["Items"] = {"[starcolor:7d7d7d][color:7d7d7d]Nothing[color:ffffff]"}
	end
end
Inventory.SetInventory(Encounter["Items"])
Encounter["NotUsedItems"] = true
Encounter["NOHIT"] = true

timer = -30

function Update()
	DialogUpdate()
	if timer < 0 then
		timer = timer + 1
	elseif Encounter["GameMusic"] == "None" and timer == 0 then
		if Encounter["phase"] == 1 then
			if not GetAlMightyGlobal("ShortDialog") then
				Encounter["GameMusic"] = "Start"
			else
				Encounter["GameMusic"] = "StartImmediate"
			end
		elseif Encounter["phase"] == -1 then
			Encounter["GameMusic"] = "ShortIntro"
		end
	else
		if not GetAlMightyGlobal("ShortDialog") and Encounter["phase"] == 1 then
			if NewAudio.GetPlayTime("One") >= 0 and timer == 0 and Black1.alpha > 0 then
				Black1.alpha = Black1.alpha - 0.1
				if Black1.alpha <= 0 then
					timer = 1
				end
			end
			if NewAudio.GetPlayTime("One") >= 0 and timer == 1 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[noskip][font:papyrus][voice:papyrus][waittall:3]Lo siento mucho.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[noskip][font:papyrus][voice:papyrus][waittall:3]I am so sorry.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				end
				timer = 2
			end
			if NewAudio.GetPlayTime("One") >= 3 and timer == 2 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[noskip][font:papyrus][voice:papyrus]Aunque hiciste\nmuchas cosas\nmalas...[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[noskip][font:papyrus][voice:papyrus]Even though you\ndid so many bad\nthings...[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				end
				timer = 3
			end
			if NewAudio.GetPlayTime("One") >= 8 and timer == 3 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[noskip][font:papyrus][voice:papyrus]Me prometí\na mí mismo\nsiempre perdonar.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[noskip][font:papyrus][voice:papyrus]I promised\nmyself to\nalways forgive.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				end
				timer = 4
			end
			if NewAudio.GetPlayTime("One") >= 12 and timer == 4 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[noskip][font:papyrus][voice:papyrus]Todos merecen\nuna segunda\noportunidad.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[noskip][font:papyrus][voice:papyrus]Everyone\ndeserve a\nsecond chance.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				end
				timer = 5
			end
			if NewAudio.GetPlayTime("One") >= 16 and timer == 5 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[noskip][font:papyrus][voice:papyrus][func:SetHead,Sans...][waittall:3]...eso incluía a\nsans.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[noskip][font:papyrus][voice:papyrus][func:SetHead,Sans...][waittall:3]...that included\nsans.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				end
				timer = 6
			end
			if NewAudio.GetPlayTime("One") >= 20 and timer == 6 then
				Bubble("", "", "", "", "", "", 1)
				timer = 7
			end
			if timer == 7 then
				if not Distorsion then
					Encounter["Legs"].layer = "Top"
					Encounter["Legs"].alpha = 0
					Encounter["Body"].alpha = 0
					Encounter["Head"].alpha = 0
					Encounter["Jaw"].alpha = 0
					Papas = CreateSprite("Papyrus/Deforme","Top")
					Distorsion = CreateSprite("Papyrus/Distorsion","Top")
					Papas.SetPivot(0.5, 0)
					Papas.SetParent(Encounter["Legs"])
					Papas.MoveTo(0, -48)
					Distorsion.alpha = 0.8
					Distorsion.MoveTo(320, 240)
					aux = 285
				end
				Papas.alpha = 0.3 + 0.1*math.random(6)
				Encounter.Call("PapsMoveTo", {320, aux})
				aux = aux + 32
				if aux >= 530 then
					aux = -190
				end
			end
			if NewAudio.GetPlayTime("One") >= 21.8 and timer == 7 then
				Encounter.Call("ResetAnim")
				Encounter["fadeGlow"] = nil
				Encounter["Legs"].alpha = 1
				Encounter["Body"].alpha = 1
				Encounter["Head"].alpha = 1
				Encounter["Jaw"].alpha = 1
				Papas.alpha = 0.3 + 0.1*math.random(6)
				Encounter["Legs"].color = {Papas.alpha,Papas.alpha,Papas.alpha}
				Encounter["Body"].color = {Papas.alpha,Papas.alpha,Papas.alpha}
				Encounter["Head"].color = {Papas.alpha,Papas.alpha,Papas.alpha}
				Encounter["Jaw"].color = {Papas.alpha,Papas.alpha,Papas.alpha}
				Encounter["Glow"].alpha = Papas.alpha
				Papas.Remove()
				Papas = nil
				Distorsion.Remove()
				Distorsion = nil
				timer = 8
				aux2 = 30
				Encounter.Call("PapsMoveTo", {320, aux})
				aux = aux + 32
				if aux >= 530 then
					aux = -190
				end
			end
			if timer == 8 then
				Encounter.Call("PapsMoveTo", {320, aux})
				local randomizador = math.max((0.3 + 0.1*math.random(6))*(NewAudio.GetPlayTime("One") - 22.645)/-0.855, 0)
				Encounter["Legs"].color = {randomizador,randomizador,randomizador}
				Encounter["Body"].color = {randomizador,randomizador,randomizador}
				Encounter["Head"].color = {randomizador,randomizador,randomizador}
				Encounter["Jaw"].color = {randomizador,randomizador,randomizador}
				Encounter["Glow"].alpha = randomizador
				aux2 = math.max(aux2 - 2, 2)
				aux = aux + aux2
				if aux >= 530 then
					aux = -190
				end
			end
		end
		if ((NewAudio.GetPlayTime("One") >= 23.5 or NewAudio.isStopped("One")) and Encounter["phase"] == 1) or ((NewAudio.GetPlayTime("One") >= 0.845 or NewAudio.isStopped("One")) and Encounter["phase"] == -1) then
			Black1.Remove()
			Black2.Remove()
			if Distorsion then
				Distorsion.Remove()
			end
			if Papas then
				Papas.Remove()
			end
			Encounter.Call("PapsMoveTo", {320, 285})
			Encounter["Legs"].layer = "Bottom"
			Encounter["Legs"].color = {1,1,1}
			Encounter["Body"].color = {1,1,1}
			Encounter["Head"].color = {1,1,1}
			Encounter["Jaw"].color = {1,1,1}
			Encounter.Call("ResetAnim")
			if Encounter["phase"] == 1 then
				if not GetAlMightyGlobal("ShortDialog") then
					Bubble("", "", "", "", "", "", 1)
				end
				SetAlMightyGlobal("ShortDialog", true)
				EndWave()
			elseif Encounter["phase"] == -1 then
				SetAlMightyGlobal("ShortDialog", true)
				Encounter["nextwaves"] = {"Wait WHAT"}
				State("DEFENDING")
			end
		end
	end
end